package se.webbzon.boltzmann.input;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

import se.webbzon.oschi01.jinput.ControllerThread;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

public class GamepadInstaller {
	
	// A collection of all the installed gamepads
	private final Collection<Input> inputs;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new empty gamepad installer. **/
	public GamepadInstaller() {
		inputs = new HashSet<Input>();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Installs all the available gamepad inputs. **/
	public void installInputs() {
		ControllerEnvironment env = ControllerEnvironment.getDefaultEnvironment();
		Controller[] controllers = env.getControllers();
		for (Controller c : controllers) {
			if (c.getType() == Controller.Type.GAMEPAD || c.getType() == Controller.Type.STICK) {
				ControllerThread t = new ControllerThread(c);
				GamepadInput input = new GamepadInput(c);
				t.addListener(input);
				t.start();
				inputs.add(input);
			}
		}
	}
	
	/** Returns a collection of all the available gamepad inputs. **/
	public Collection<Input> getInputs() {
		return Collections.unmodifiableCollection(inputs);
	}

}
